<?php
/**
* @link http://framework.org/
* @version 0.7.0
* @copyright Copyright: 2002-2005 Doutromundo
* @author Gregory Brown <bugzbrown@gmail.com>
* @access public
* @package core

*/
/**
* Pager class - Not really sure which of these is valid, so bare with me
* @package core
* @subpackage paginator
*/
class pager{

	// PUBLIC ATTRIBUTES

	var $items_per_page = 0;
	var $total_records = 0;
	var $next_page = 0;
	var $prev_page = 0;
	var $last_page = 0;
	var $in_object = ""; // array of elements...
	var $out_object = ""; // array of elements...

	// VAR PRIVATE ATTRIBUTES
	var $internal_current_page = 0;

	// VAR FUNCIONTS
	function set_items_per_page($items_per_page){
		$this->items_per_page = $items_per_page;
	}
	function set_in_object($in_object){
		$this->in_object=$in_object;
		$this->total_records=count($in_object);
	}

	function current_page($page=""){
		if ($page==""){
			// NO PAGE - SO WE DISPLAY
			return $this->internal_current_page+1;
		}
		if (is_int($page)){
			//number so we set
			$this->internal_current_page=$page;
		}
	}

	function pageOutput(){
		$t = $this->total_records;
		$c = $this->internal_current_page;
		$i = $this->items_per_page;
		$startLoop = $i*$c;
		$endLoop = $t;
		$lastPage = ceil($t/$i)>1?ceil($t/$i):1;
		$prvPag = $c>0?$c-1:0;
		$nxtPag = $c<$lastPage?$c+1:$lastPage;
		$nextCount = ($nxtPag*$i)<$t?($nxtPag*$i):$t;

		// loop through the object and generate a new array with only the necessary items.
		$tmpA = array();
		for ($j=($c*$i);$j<$nextCount;$j++){
			array_push($tmpA,$this->in_object[$j]);
		}
		$this->next_page=$nxtPag;
		$this->prev_page=$prvPag;
		$this->last_page=$lastPage;
		$this->out_object=$tmpA;

	}

	function pager($items_per_page=0, $in_object="", $currentPage=""){
		if ($items_per_page!=0){
			$this->set_items_per_page($items_per_page);
		}
		if ($in_object!=""){
			$this->set_in_object($in_object);
		}
		if ($currentPage!=""){
			$this->internal_current_page=$currentPage;
		}
	}

}

?>